<script lang="ts" setup>
import { reactive, onMounted, ref, computed } from 'vue'
import PrintView from './PrintView.vue'
import InputNumber from '../form/Input/InputNumber.vue'

defineOptions({
  inheritAttrs: false
})
const emit = defineEmits(['close'])
const props = defineProps({
  sendParams: {
    type: Object,
    default: function() {
      return null
    }
  },
  total: {
    type: Number,
    default: 0
  },
  title: {
    type: String,
    default: '打印信息'
  }
})

const state:any = reactive({
  isLoading: false,
  showPrint: false,
  formData: {
    pageSize: 0
  },
  rules: {

  },
  roleMenus: [],
  params: null,
  listViewId: ''
})

const loadingHandler = () => {

}
const errorHandler = () => {
  state.isLoading = false
}


const submitHandler = () => {
  state.params = { ...(props.sendParams || {}), pageSize: state.formData.pageSize }
  state.showPrint = true
  state.isLoading = true
}

const readyHandler = () => {
  state.isLoading = false
}

const closeHandler = () => {
  emit('close')
}

onMounted(() => {
  if (props.sendParams && props.sendParams.pageSize) {
    state.formData.pageSize = props.sendParams.pageSize
  }
})

</script>

<template>
  <v-pop-win :title="title"
             width="450"
             ok-label="打 印"
             :loading="state.isLoading"
             @close="closeHandler"
             @submit="submitHandler()">
    <el-form ref="formRef"
             :style="{width:'100%'}"
             :model="state.formData"
             :rules="state.rules"
             class="form-box"
             :label-width="100"
             status-icon>
      <el-form-item label="打印条数" prop="pageSize">
        <div class="hl-item">
          <input-number v-model.trim="state.formData.pageSize"
                        placeholder="请输入打印条数"
                        end="条"
                        :min="1"
                        :max="total" />
          <div class="total-num">
            总计 {{ total }} 条
          </div>
        </div>
      </el-form-item>
    </el-form>
    <print-view v-if="state.showPrint"
                v-bind="$attrs"
                :params="state.params"
                @error="errorHandler"
                @ready="readyHandler"
                @close="closeHandler">
      <slot />
    </print-view>
  </v-pop-win>
</template>

<style scoped lang="scss">
.form-box{
  padding: 20px 0 10px 0;
  box-sizing: border-box;
}
.hl-item{
  @include hor-group();
  width:100%;
}
.total-num{
  flex:none;
  font-size:12px;
  padding-left:10px;
  box-sizing: border-box;
}
</style>
